package 设计模式.p_观察者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * @author caihe
 * @date 2023/3/21 16:01
 */
public class Weather {

    private WeatherType currentWeather;
    private final List<WeatherObserver> observers;

    public Weather() {
        observers = new ArrayList<>();
        currentWeather = WeatherType.SUNNY;
    }

    public void addObserver(WeatherObserver weatherObserver) {
        observers.add(weatherObserver);
    }

    public void removeObserver(WeatherObserver weatherObserver) {
        observers.remove(weatherObserver);
    }
    
    public void timePasses() {
        WeatherType[] values = WeatherType.values();
        currentWeather = values[(currentWeather.ordinal() + 1) % values.length];
        notifyObservers();
    }
    
    private void notifyObservers() {
        for (WeatherObserver observer : observers) {
            observer.update(currentWeather);   
        }
    }

}
